package com.bbms.controller.borrowRecordManage;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bbms.entity.BorrowRecord;
import com.bbms.entity.ErrorCode;
import com.bbms.entity.Page;
import com.bbms.entity.Result;
import com.bbms.entity.SuccessCode;
import com.bbms.service.BorrowRecordService;
import com.bbms.util.StringUtil;


/**
 * 图书借阅管理控制器
 * @author Administrator
 */
@Controller
@RequestMapping("/bbms/borrowRecordManage")
public class BorrowRecordManageController {

	@Resource(name="BorrowRecordServiceImpl")
	private BorrowRecordService brs;
	
	@RequestMapping("/borrowRecordManage")
	public String borrowRecordManage(){
		return "borrowRecordManage/borrowRecordManage.html";
	}
	
	
	/**
	 * 获取分页数据
	 * @param page 当前页
	 * @param rows 数量
	 * @param s_book 书名
	 * @param s_reader 读者名
	 * @param s_manager 管理员名
	 * @param s_renewBorrowNum 续借次数
	 * @param s_status 状态
	 * @param borrowStartTime 借书开始时间
	 * @param borrowEndTime 借书结束时间
	 * @param restoreStartTime 还书开始时间
	 * @param restoreEndTime 还书结束时间
	 * @return 分页数据
	 */
	@ResponseBody
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public Page getPage(int page, int rows, String s_book, String s_reader, String s_manager,
			Integer s_renewBorrowNum, Integer s_status, String borrowStartTime, String borrowEndTime,
			String restoreStartTime, String restoreEndTime){
		
		System.out.println("参数："+"   "+page  + rows  + s_book  + s_reader + s_manager + s_renewBorrowNum +
							s_status + borrowStartTime + borrowEndTime + restoreStartTime + restoreEndTime);
		
		Page data=new Page();
		
		data=brs.getPage(page, rows, s_book, s_reader, s_manager, s_renewBorrowNum, 
							s_status, borrowStartTime, borrowEndTime, restoreStartTime, restoreEndTime);
		
		return data;
		
	}
	
	/**
	 * 借阅图书、修改借阅
	 * @param record 借阅记录对象
	 * @return 反馈信息
	 */
	@ResponseBody
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public Result save(@ModelAttribute("record")BorrowRecord record, HttpServletRequest request){
		
		Result result=new Result();
		
		HttpSession session=request.getSession();
		String managerAccount=(String) session.getAttribute("user");
		int managerID=brs.getCurrentManagerId(managerAccount);		//管理员ID
		
		System.out.println("用户账号为： "+managerAccount+"	"+"id为："+managerID);
		
		System.out.println(record.toString());
		
		if (record.getId()==0) {
			
			try{
				record.setManagerId(managerID);
				//是否借阅成功
				if (brs.insertRecord(record)) {
					result.setStatus(SuccessCode.borrowSuccess.getCode());
					result.setMsg(SuccessCode.borrowSuccess.getName());
				}else{
					result.setStatus(ErrorCode.readerBorrowFailed.getCode());
					result.setMsg(ErrorCode.readerBorrowFailed.getName());
				}
				
			}catch(Exception e){
				e.printStackTrace();
				result.setStatus(ErrorCode.borrowFailed.getCode());
				result.setMsg(ErrorCode.borrowFailed.getName());
			}
		}else{
			
			try {
				record.setManagerId(managerID);
				
				//是否修改成功
				if (brs.updateRecord(record)) {
					result.setStatus(SuccessCode.updateSuccess.getCode());
					result.setMsg(SuccessCode.updateSuccess.getName());
				}else {
					result.setStatus(ErrorCode.readerBorrowFailed.getCode());
					result.setMsg(ErrorCode.readerBorrowFailed.getName());
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(ErrorCode.updateFailed.getCode());
				result.setMsg(ErrorCode.updateFailed.getName());
			}
		}
		
		return result;
	}
	
	/**
	 * 续借图书
	 * @param record 借阅记录对象
	 * @return 反馈信息
	 */
	@ResponseBody
	@RequestMapping(value="renewBorrow", method=RequestMethod.POST)
	public Result renewBorrow(@ModelAttribute("record")BorrowRecord record, HttpServletRequest request){
		Result result=new Result();
		
		HttpSession session=request.getSession();
		String managerAccount=(String) session.getAttribute("user");
		int managerID=brs.getCurrentManagerId(managerAccount);		//管理员ID
		
		System.out.println(record.toString());
		
		record.setManagerId(managerID);
		try {
			
			if (brs.renewBorrow(record)) {
				result.setStatus(SuccessCode.renewBorrowSuccess.getCode());
				result.setMsg(SuccessCode.renewBorrowSuccess.getName());
			}else{
				result.setStatus(ErrorCode.readerRenewBorrowFaild.getCode());
				result.setMsg(ErrorCode.readerRenewBorrowFaild.getName());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(ErrorCode.renewBorrowFaild.getCode());
			result.setMsg(ErrorCode.renewBorrowFaild.getName());
		}
		
		return result;
	}
	
	/**
	 * 归还图书（单一）
	 * @param record 借阅记录对象
	 * @return 反馈信息
	 */
	@ResponseBody
	@RequestMapping(value="singleDel", method=RequestMethod.POST)
	public Result singleDel(@ModelAttribute("record")BorrowRecord record, HttpServletRequest request){
		Result result=new Result();
		
		HttpSession session=request.getSession();
		String managerAccount=(String) session.getAttribute("user");
		int managerID=brs.getCurrentManagerId(managerAccount);		//管理员ID
		record.setManagerId(managerID);
		
		try {
			
			brs.delete(record);
			result.setStatus(SuccessCode.restoreSuccess.getCode());
			result.setMsg(SuccessCode.restoreSuccess.getName());
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(ErrorCode.restoreFaild.getCode());
			result.setMsg(ErrorCode.restoreFaild.getName());
		}
		
		return result;
	}
	
	/**
	 * 批量归还图书
	 * @param list 将要归还图书序列
	 * @return 反馈结果
	 */
	@ResponseBody
	@RequestMapping(value="batchDel", method=RequestMethod.POST)
	public Result batchDel(String ids, HttpServletRequest request){
		Result result=new Result();
		
		HttpSession session=request.getSession();
		String managerAccount=(String) session.getAttribute("user");
		int managerID=brs.getCurrentManagerId(managerAccount);		//管理员ID
		
		String[] strings = ids.split(",");
		List<Integer> list = new ArrayList<Integer>();
		
		for (int i = 0; i < strings.length; i++) {
			if (StringUtil.isNotEmpty(strings[i])){
				list.add(Integer.parseInt(strings[i]));
			}
		}
		
		System.out.println("========"+list.toString()+"====================");
		
		for (int i = 0; i < list.size(); i++) {
			System.out.println("========"+list.get(i).toString()+"====================");
		}
		
		try {
			 
			brs.bacthDel(list,managerID);
			result.setStatus(SuccessCode.restoreSuccess.getCode());
			result.setMsg(SuccessCode.restoreSuccess.getName());
			
		} catch (Exception e) {
			
			e.printStackTrace();
			result.setStatus(ErrorCode.restoreFaild.getCode());
			result.setMsg(ErrorCode.restoreFaild.getName());
		}

		return result;
	}
	
	
}
